from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadData


def generate_openid_signature(openid):
    """加密"""
    serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=600)
    data = {'openid': openid}
    openid_sign = serializer.dumps(data)
    return openid_sign.decode()


def check_openid(openid_sign):
    """对openid解密"""
    serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=600)
    try:
        data = serializer.loads(openid_sign)
    except BadData:
        return None
    return data.get('openid')

















